<template>
  <div class="app-container">
    <!-- 头 -->
    <Find @inputDBclick="inputDBclick" :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findData" @Launchform="Launchform" />

    <!-- 体 -->
    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <!-- 左 -->
        <div class="MainLeft">
          <div>
            <div style="width: 99%">
              <h1>型体</h1>
              <Table
                :isLoading="isLoading"
                @currentRow="currentRow1"
                :tableData="TypeQueryMethodsTableDateList"
                :tableHeader="tableHeader"
                :TableHeight="50"
              />
              <PageModule
                :PageData="{ total: totalTk, buttonS: true, pageSizeShow: true, small: true }"
                :layout="'prev, pager'"
                @pageSizeChange="handleSizeChangeTk"
                @pageChange="handleCurrentChangeTk"
              />
            </div>
            <div style="width: 99%">
              <h1>代码</h1>
              <Table
                :isLoading="isLoading2"
                @currentRow="currentRow2"
                :tableData="ColorCodePictureMethodsParmsTableDateList.list"
                :tableHeader="tableHeader1"
                :TableHeight="42"
              />
              <PageModule
                :PageData="{ total: total, buttonS: true, pageSizeShow: true }"
                :layout="'prev, pager'"
                @pageSizeChange="handleSizeChange"
                @pageChange="handleCurrentChange"
              />
            </div>
          </div>
        </div>

        <div class="MainRight">
          <div style="width: 98%">
            <h1>用量明细资料</h1>
            <Table
              @inputChange="confirmChange20"
              @emitSelectValue="selectValChange"
              @currentRow1="currentRow20"
              :isLoading="isLoading3"
              @selectListShow="selectListShow"
              @selectList="selectedList"
              @currentRow="currentRow3"
              :tableData="TypeColorDataQueryMethodsInterfaceTableDateList"
              :selection="true"
              :tableHeader="tableHeader3"
              :TableHeight="50"
            />
          </div>
          <div class="MainRightLeft" style="margin-top: 10px">
            <div style="width: 60%">
              <h1>贴合子材料明细</h1>
              <Table
                @inputChange="confirmChange10"
                :isLoading="isLoading4"
                @checkboxChange="change10"
                :tableData="QueryTheDetailsOfTheFitSubMaterialMethodsParmsTableDateList"
                :tableHeader="tableHeader5"
                :TableHeight="50"
              />
            </div>
            <div style="width: 20%; margin-left: 50px">
              <h1>全码用量资料</h1>
              <el-input
                placeholder="请输入"
                style="width: 90px; margin-top: 10px"
                ref="InputRef"
                v-model="LevelBasicNumberMethodsParms.baseSize"
                size="small"
              />
              <Table
                :isLoading="isLoading5"
                @currentRow="currentRow10"
                :tableData="AllListMethodsTableDateList"
                :tableHeader="tableHeader6"
                :TableHeight="50"
              />
            </div>
          </div>
        </div>
      </div>
    </el-card>

    <el-dialog :close-on-click-modal="false" v-model="dialogVisible" :direction="'ttb'" size="small" :with-header="false">
      <Table :tableData="PrecautionsFileMethodsParmsDateList" :tableHeader="tableHeader19" :TableHeight="80" />
      <div style="margin: auto; width: 55px">
        <el-button type="primary" size="default" @click="inputChange">保存</el-button>
      </div>
    </el-dialog>

    <!-- 选择材料 -->
    <el-dialog :close-on-click-modal="false" v-model="dialogVisible1" :direction="'ttb'" size="small" :with-header="false">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList1" @Launchform="Launchform1" @findClicked="findData1" />
      <h1>正式材料</h1>
      <Table
        @currentRow="currentRow7"
        :isLoading="isLoading1"
        :tableHeader="tableHeader2"
        :tableData="FormalMaterialsAndMethodsForDevelopingTableDateList.officialMaterials.adhesiveMaterialList"
        :TableHeight="50"
        :outTableHeight="60"
        @currentRow1="currentRow1"
      />
      <PageModule
        style="margin-left: 10px"
        :PageData="{ buttonS: true, total: totalT, pageSizeShow: true }"
        @pageChange="handleSizeChange1"
        @pageSizeChange="handleCurrentChange1"
      />
    </el-dialog>

    <!-- 复制用量 -->
    <el-dialog :close-on-click-modal="false" v-model="dialogVisible5" title="选择材料" width="30%" class="dialog pb0">
      <div>
        <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList8" @Launchform="Launchform10" />
        <div style="width: 70%; margin-top: 20px">
          <Table
            @selectList="selectedList12"
            :selection="true"
            :tableHeader="tableHeader16"
            :tableData="ReplicationVolumeQueryTableDateList.list"
            :TableHeight="40"
            :outTableHeight="230"
          />
          <PageModule
            :PageData="{ total: totalActiveList, buttonS: true, pageSizeShow: true }"
            :layout="'prev, pager'"
            @pageSizeChange="handleSizeChangeTkList"
            @pageChange="handleCurrentChangeTkList"
          />
        </div>
        <h1 style="color: #0000ff">把当前型体颜色用量复制到以以上打勾型体颜色</h1>
        <div style="width: 30px; margin: auto; margin-top: 20px">
          <el-button type="primary" size="default" @click="ReplicationDosageMethods">复制</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 全码用量资料明细 -->
    <el-dialog :close-on-click-modal="false" v-model="AllSizeNo1" :direction="'ttb'" size="small" :with-header="false" width="30%">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList15" @Launchform="Launchform20" @findClicked="findClicked20" />
    </el-dialog>

    <!-- 全码用量资料明细 -->
    <el-dialog :close-on-click-modal="false" v-model="AllSizeNo2" :direction="'ttb'" size="small" :with-header="false" width="30%">
      <Table
        :isLoading="isLoading2"
        @currentRow1="currentRow15"
        :tableData="GetColorOrColorCodeAndImagTableData"
        :tableHeader="tableHeader1"
        :TableHeight="42"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import LoginWork from '@/utils/LoginWork'
import {
  FormalMaterialsAndDevelopmentMaterials,
  type FormalMaterialsAndDevelopmentMaterialsInterface,
  type FormalMaterialsAndDevelopmentMaterialsParms
} from '@/api/ControlDepartment/MaterialManagement/LaminatedMaterialFile'
import { useRouter } from 'vue-router'
import { PositionDetailQuery } from '@/api/ControlDepartment/MaterialManagement/ResearchAndDevelopmentVolumeDocumentation'
import type { PositionDetailQueryPamrs } from '@/api/ControlDepartment/MaterialManagement/ResearchAndDevelopmentVolumeDocumentation'
import { GetVendorList } from '@/api/WarehouseDepartment/FactoryStorage/InputInsoleReceivingIssuingMaterials'
import type { GetVendorListListParms } from '@/api/WarehouseDepartment/FactoryStorage/InputBottomReceivingIssuingMaterials'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'BulkQuantityFile'
})
</script>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import {
  ubdateSizeNo,
  GetColorOrColorCodeAndImag,
  ColorCodeUpdate,
  LevelBasicNumber,
  bomDataDeletion,
  SelectTheCurrentRowToInsert,
  FetchResearchBom,
  DeleteTheCheckmarkedRecordType,
  FullCodeUsageCheck,
  FullCodeUsageData,
  QueryTheDetailsOfTheFitSubMaterial,
  TypeColorDataQuery,
  TypeQuery,
  ColorCodeQuery,
  UpdateFullCodeUsage,
  PrecautionsFileList,
  PrecautionsFile,
  saveDateList
} from '@/api/ControlDepartment/MaterialManagement/BulkQuantityFile'
import type {
  LevelBasicNumberPamrs,
  SelectTheCurrentRowToInsertParms,
  DeleteTheCheckmarkedRecordTypePamrs,
  FullCodeUsageDataParms,
  LaminatedSubmaterialInterface,
  TypeColorDataQueryParms,
  TypeColorDataQueryInterface,
  TypeQueryParms,
  ColorCodePictureTableDate,
  ColorCodePictureParms,
  UpdateFullCodeUsagePamrs
} from '@/api/ControlDepartment/MaterialManagement/BulkQuantityFile'

// 修改基本号
const ubdateSizeNoMethodsParms = ref({
  baseSize: '',
  styleColor: '',
  styleColorMaterialRequests: [],
  styleColorRemark: '',
  styleNo: ''
})

const ubdateSizeNoMethods = async () => {
  const result = await ubdateSizeNo(ubdateSizeNoMethodsParms.value)
  if (result.code === 200) {
    ColorCodeQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 获取颜色
const GetColorOrColorCodeAndImagParms = ref({
  styleNo: ''
})

const GetColorOrColorCodeAndImagTableData = ref([])

const GetColorOrColorCodeAndImagMethods = async () => {
  const result = await GetColorOrColorCodeAndImag(GetColorOrColorCodeAndImagParms.value)
  GetColorOrColorCodeAndImagTableData.value = result
}

// 双击查看颜色名称
const AllSizeNo2 = ref(false)

const inputDBclick = () => {
  GetColorOrColorCodeAndImagMethods()
  AllSizeNo2.value = true
}

const currentRow15 = (row: any) => {
  AllSizeNo2.value = false
  formList.value[2].inputValue = row.styleColor
  formList.value[1].inputValue = row.colorCode
}

const confirmChange20 = (targetRow: any) => {}

const formList = ref([
  {
    type: 'input',
    inputLabel: '型体',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '颜色代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '颜色名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  },
  {
    type: 'button',
    checkboxLabel: '更新全码用量'
  },
  {
    type: 'button',
    checkboxLabel: '保存',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '取研发BOM',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '检核',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '注意事项建档'
  },
  {
    type: 'button',
    checkboxLabel: 'BOM表导出'
  },
  {
    type: 'button',
    checkboxLabel: '增加明细资料'
  },
  {
    type: 'button',
    checkboxLabel: '重排序号'
  },
  {
    type: 'button',
    checkboxLabel: '选择材料'
  },
  {
    type: 'button',
    checkboxLabel: '删除勾选部位资料'
  },
  {
    type: 'button',
    checkboxLabel: '复制用量'
  },
  {
    type: 'button',
    checkboxLabel: 'BOM资料删除'
  },
  {
    type: 'button',
    checkboxLabel: '转型体资料建档'
  },
  {
    type: 'button',
    checkboxLabel: '转指令资料输入'
  },
  // {
  //   type: 'button',
  //   checkboxLabel: '颜色代码更新',
  // },
  {
    type: 'button',
    checkboxLabel: '修改请购标志'
  },
  {
    type: 'button',
    checkboxLabel: '级放部位全码用量'
  },
  {
    type: 'button',
    checkboxLabel: '新增全码用量'
  },
  {
    type: 'button',
    checkboxLabel: '删除全码用量'
  },
  {
    type: 'input',
    inputLabel: '型体基本号码',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存型体基本号码'
  }
])

const selectValChange = (row: any, bull: any) => {
  bull.partName.selectValue = row
  PositionDetailQueryMethodsParms.value.partName = row
  PositionDetailQueryMehtods(bull)
}

const currentRow20 = (row: any) => {
  AllSizeNo1.value = true
  formList15.value[0].inputValue = row.seqNo
  AllSizeNo13.value = row.seqNo
  formList15.value[1].inputValue = ''
}

// 序号变更
const AllSizeNo1 = ref(false)

const AllSizeNo13 = ref()

const newSeqNo = ref()

const Launchform20 = (row: any) => {
  newSeqNo.value = row[1].inputValue
}

const findClicked20 = (row: any) => {
  if (row === '确认调整') {
    if (newSeqNo.value === '') {
      JMessage.warning('新序号不可为空')
    } else {
      if (newSeqNo.value === formList15.value[0].inputValue) {
        JMessage.warning('新序号不可和就序号相同')
      } else {
        const index = TypeColorDataQueryMethodsInterfaceTableDateList.value.findIndex((item) => item.seqNo === newSeqNo.value)

        if (index !== -1) {
          // 获取目标索引（将取目标当前元素的前一个元素）
          const targetIndex = AllSizeNo13.value - 1

          // 确保目标索引是有效的
          if (targetIndex >= 0 && targetIndex < TypeColorDataQueryMethodsInterfaceTableDateList.value.length) {
            const target = TypeColorDataQueryMethodsInterfaceTableDateList.value[targetIndex] // 获取目标元素
            const source = TypeColorDataQueryMethodsInterfaceTableDateList.value[index] // 获取源元素

            // 交换元素（除了 seqNo）
            TypeColorDataQueryMethodsInterfaceTableDateList.value[targetIndex] = {
              ...source, // 源对象
              seqNo: target.seqNo // 保持目标的 seqNo
            }

            TypeColorDataQueryMethodsInterfaceTableDateList.value[index] = {
              ...target, // 目标对象
              seqNo: source.seqNo // 保持源的 seqNo
            }
          }
        }
        AllSizeNo1.value = false
      }
    }
  }
}

const formList15 = ref([
  {
    type: 'input1',
    inputValue: '',
    inputPlaceholder: '旧序号',
    inputDisabled: true
  },
  {
    type: 'input',
    inputValue: '',
    inputPlaceholder: '新序号'
  },
  {
    type: 'button',
    checkboxLabel: '确认调整'
  }
])

const tableDateList = ref()

const formList8 = ref([
  {
    type: 'input',
    inputValue: '',
    inputPlaceholder: '型体'
  }
])

const totalListpartName = ref(0)

const isLoading = ref(true)
const isLoading2 = ref(true)
const isLoading3 = ref(true)
const isLoading4 = ref(true)
const isLoading5 = ref(true)

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

// 贴合子材料明细修改
const listArrayDate = ref()

const mtlNoYes = ref()

const change10 = (row: any, list: any) => {
  ElMessageBox.confirm('请确认要修改当前记录的是否请购标志吗？')
    .then(async () => {
      confirmChange10(list)
    })
    .catch(() => {
      list.purFlag = !list.purFlag
      list = list
    })
}

const confirmChange10 = async (row: any) => {
  row.styleNo = ColorCodeQueryMethodsParms.value.styleNo
  row.styleColor = ColorCodeQueryMethodsParms.value.styleColor
  row.mtlNo = mtlNoYes.value
  row.purFlag = row.purFlag ? 0 : 1
  listArrayDate.value = row.purFlag
  const result = await LoginWork.post<any, any>(`/Cargo/getDetailsOfAdhesiveSubMaterialUpdate/${listArrayDate.value}`, row)
  if (result.code === 200) {
    QueryTheDetailsOfTheFitSubMaterialMethods()
    JMessage.success(result.message)
  } else {
    QueryTheDetailsOfTheFitSubMaterialMethods()
    JMessage.error(result.message)
  }
}

// 部位明细
const PositionDetailQueryMethodsParms = ref<PositionDetailQueryPamrs>({
  engPartName: '',
  pageNum: 1,
  pageSize: 40,
  partName: '',
  seqNo: ''
})

const listanyTableDateSelect = ref([])

const PositionDetailQueryMehtods = async (row?: any) => {
  let NewArray = []
  const result = await PositionDetailQuery(PositionDetailQueryMethodsParms.value)
  NewArray = result.partList.reduce((arr: any, item: any) => {
    if (item.partName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.partName)
    }
    return arr
  }, [])
  AddDetailData.value.partName.options = EvaluatesTheArrayReturnValue1(NewArray)
  listanyTableDateSelect.value = EvaluatesTheArrayReturnValue1(NewArray)
  totalListpartName.value = result.total
}

const vableState = ref(0)

const VendorListValue = ref([])

const totalListArray = ref(0)

const GetVendorListListParmsMethods = ref<GetVendorListListParms>({
  pageNum: 1,
  pageSize: 40
})

const GetVendorListMethods = async () => {
  let NewArray = []
  const result = await GetVendorList(GetVendorListListParmsMethods.value)
  NewArray = result.dtos.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  AddDetailData.value.vendor.options = EvaluatesTheArrayReturnValue1(NewArray)
  VendorListValue.value = EvaluatesTheArrayReturnValue1(NewArray)
  totalListArray.value = result.total
  selectListShow()
}

const selectListShow = (row?: any, visible?: boolean) => {
  if (vableState.value === 1) {
    if (visible === true) {
      if (row !== undefined) {
        row.partName = {
          selectValue: row.partName.selectValue,
          options: listanyTableDateSelect,
          PageModule: true,
          total: totalListpartName,
          handleSizeChange: (val: any) => {
            PositionDetailQueryMethodsParms.value.pageNum = val
            PositionDetailQueryMehtods()
          },
          handleCurrentChange: (val: any) => {
            PositionDetailQueryMethodsParms.value.pageNum = val
            PositionDetailQueryMehtods()
          }
        }
        row.vendor = {
          selectValue: row.vendor.selectValue,
          options: VendorListValue,
          PageModule: true,
          total: totalListArray,
          handleSizeChange: (val: any) => {
            GetVendorListListParmsMethods.value.pageNum = val
            GetVendorListMethods()
          },
          handleCurrentChange: (val: any) => {
            GetVendorListListParmsMethods.value.pageNum = val
            GetVendorListMethods()
          }
        }
        row.usageType = {
          selectValue: row.usageType.selectValue,
          options: [
            { value: '全码', label: '全码' },
            { value: '基本码', label: '基本码' },
            { value: '中间码', label: '中间码' }
          ]
        }
        row.drawDept = {
          selectValue: row.drawDept.selectValue,
          options: [
            { value: '冲裁', label: '冲裁' },
            { value: '外协', label: '外协' },
            { value: '电脑车', label: '电脑车' },
            { value: '针车', label: '针车' },
            { value: '成型', label: '成型' }
          ]
        }
        row.mtlProperty = {
          selectValue: row.mtlProperty.selectValue,
          options: [
            { value: '冲裁主料', label: '冲裁主料' },
            { value: '冲裁辅料', label: '冲裁辅料' },
            { value: '针车辅料', label: '针车辅料' }
          ]
        }
      } else {
        if (row.partName === Object) {
          row.partName.options = []
          row.vendor.options = []
          row.usageType.options = []
          row.drawDept.options = []
          row.mtlProperty.options = []
        }
      }
    }
  } else {
    if (visible === true) {
      if (row !== undefined) {
        PositionDetailQueryMethodsParms.value.partName = row.partName.selectValue

        PositionDetailQueryMehtods()
        row.partName = {
          selectValue: row.partName.selectValue,
          options: listanyTableDateSelect,
          PageModule: true,
          total: totalListpartName,
          handleSizeChange: (val: any) => {
            PositionDetailQueryMethodsParms.value.pageNum = val
            PositionDetailQueryMehtods()
          },
          handleCurrentChange: (val: any) => {
            PositionDetailQueryMethodsParms.value.pageNum = val
            PositionDetailQueryMehtods()
          }
        }
        row.vendor = {
          selectValue: row.vendor.selectValue,
          options: VendorListValue,
          PageModule: true,
          total: totalListArray,
          handleSizeChange: (val: any) => {
            GetVendorListListParmsMethods.value.pageNum = val
            GetVendorListMethods()
          },
          handleCurrentChange: (val: any) => {
            GetVendorListListParmsMethods.value.pageNum = val
            GetVendorListMethods()
          }
        }
        row.usageType = {
          selectValue: row.usageType.selectValue,
          options: [
            { value: '全码', label: '全码' },
            { value: '基本码', label: '基本码' },
            { value: '中间码', label: '中间码' }
          ]
        }
        row.drawDept = {
          selectValue: row.drawDept.selectValue,
          options: [
            { value: '冲裁', label: '冲裁' },
            { value: '外协', label: '外协' },
            { value: '电脑车', label: '电脑车' },
            { value: '针车', label: '针车' },
            { value: '成型', label: '成型' }
          ]
        }
        row.mtlProperty = {
          selectValue: row.mtlProperty.selectValue,
          options: [
            { value: '冲裁主料', label: '冲裁主料' },
            { value: '冲裁辅料', label: '冲裁辅料' },
            { value: '针车辅料', label: '针车辅料' }
          ]
        }
      }
    } else {
      if (row !== undefined && row.partName === Object) {
        row.partName.options = []
        row.vendor.options = []
        row.usageType.options = []
        row.drawDept.options = []
        row.mtlProperty.options = []
      }
    }
  }
}

// 级放基本号码
const LevelBasicNumberMethodsParms = ref<LevelBasicNumberPamrs>({
  baseSize: '',
  partName: '',
  segment: -1,
  styleColor: '',
  styleNo: '',
  usage: -1,
  usageType: ''
})

const LevelBasicNumberMethods = async () => {
  LevelBasicNumberMethodsParms.value.baseSize = Number(LevelBasicNumberMethodsParms.value.baseSize)
  const result = await LevelBasicNumber(LevelBasicNumberMethodsParms.value)
  if (result.code === 200) {
    TypeColorDataQueryMethods()
    JMessage.success(result.message)
  } else {
    TypeColorDataQueryMethods()
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlcLtvCodeUsage2 = (_done?: () => void) => {
  ElMessageBox.confirm('请确认要级放该全码用量吗?')
    .then(async () => {
      LevelBasicNumberMethods()
    })
    .catch(() => {})
}

// BOM资料删除
const cfmFlagListDelete = ref('')

const bomDataDeletionMethods = async () => {
  const result = await bomDataDeletion({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    cfmFlag: cfmFlagListDelete.value
  })
  if (result.code === 200) {
    TypeColorDataQueryMethods()
    ColorCodeQueryMethods()
    JMessage.success(result.message)
  } else {
    TypeColorDataQueryMethods()
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlcLtvCodeUsage1 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      bomDataDeletionMethods()
    })
    .catch(() => {})
}

// 复制用量
const ReplicationDosageMethodsParms = ref<any>([])

const selectedList12 = (row: any) => {
  ReplicationDosageMethodsParms.value = row.map((item: any) => {
    return item
  })
}

const ReplicationDosageMethods = async () => {
  const result = await LoginWork.post<any, any>(`/Cargo/getCopyingDosage`, {
    currentStyleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    currentStyleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    list: ReplicationDosageMethodsParms.value
  })
  if (result.code === 200) {
    dialogVisible5.value = false
    JMessage.success(result.message)
  } else {
    dialogVisible5.value = false
    JMessage.error(result.message)
  }
}

// 复制用量查询列表
const dialogVisible5 = ref(false)

const ReplicationVolumeQueryTableDateList = ref<any>({
  list: []
})

const totalActiveList = ref(0)

const Launchform10 = (row: any) => {
  ReplicationVolumeQueryTableDateListParms.value.styleNo = row[0].inputValue
  ReplicationVolumeQuery()
}

const ReplicationVolumeQueryTableDateListParms = ref<any>({
  pageNum: 1,
  pageSize: 20,
  styleNo: ''
})

const ReplicationVolumeQuery = async () => {
  const result = await LoginWork.post<any, any>(
    `/Cargo/getCopyingDosageList`,
    Object.assign(
      {},
      { currentStyleColor: FullCodeUsageCheckMethodsParms.value.styleColor, currentStyleNo: FullCodeUsageCheckMethodsParms.value.styleNo },
      ReplicationVolumeQueryTableDateListParms.value
    )
  )
  ReplicationVolumeQueryTableDateList.value.list = result.data.list
  totalActiveList.value = result.data.total
}

const handleSizeChangeTkList = (val: number) => {
  ReplicationVolumeQueryTableDateListParms.value.pageNum = val
  ReplicationVolumeQuery()
}

const handleCurrentChangeTkList = (val: number) => {
  ReplicationVolumeQueryTableDateListParms.value.pageNum = val
  ReplicationVolumeQuery()
}

// 保存
const saveDateListMethods = async () => {
  const partNameSegments = new Map() // 用于存储每个 partName 的 segment 值
  TypeColorDataQueryMethodsInterfaceTableDateList.value = TypeColorDataQueryMethodsInterfaceTableDateList.value.filter(
    (item: any) => item.partName.selectValue
  )
  const result = await saveDateList({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    styleColorMaterialRequests: TypeColorDataQueryMethodsInterfaceTableDateList.value.map((item: any) => {
      item.drawDept = item.drawDept.selectValue
      item.mtlProperty = item.mtlProperty.selectValue
      item.partName = item.partName.selectValue
      item.usageType = item.usageType.selectValue
      item.vendor = item.vendor.selectValue
      item.noLotUsage = item.noLotUsage === true ? '0' : null

      // 检查 partName 是否已经存在
      if (partNameSegments.has(item.partName)) {
        // 如果 partName 已存在，segment 在原有基础上增加 1
        item.segment = partNameSegments.get(item.partName) + 1
      } else {
        // 如果 partName 不存在，segment 设置为 0
        item.segment = 0
      }

      // 更新 partName 的 segment 值
      partNameSegments.set(item.partName, item.segment)

      // 检查 usage 是否有效
      if (item.usage === '' || isNaN(item.usage) || item.usage === 0) {
        item.usage = null
      } else {
      }

      return item
    }),
    styleColorRemark: ''
  })
  if (result.code === 200) {
    AddDetailData.value.mtlNo = ''
    TypeColorDataQueryMethods()
    JMessage.success(result.message)
  } else {
    AddDetailData.value.mtlNo = ''
    TypeColorDataQueryMethods()
    JMessage.error(result.message)
  }
}

// 复制当前材料到勾选部位
const AddDetailData = ref({
  seqNo: '',
  partName: {
    selectValue: '',
    options: []
  },
  segment: '',
  mtlNo: '',
  mtlColor: '',
  mtlName: '',
  mtlSoec: '',
  mtlUnit: '',
  size1: '',
  size2: '',
  vendor: {
    selectValue: '',
    options: []
  },
  usage: '',
  usageType: {
    selectValue: '中间码',
    options: [
      { value: '全码', label: '全码' },
      { value: '基本码', label: '基本码' },
      { value: '中间码', label: '中间码' }
    ]
  },
  drawDept: {
    selectValue: '',
    options: [
      { value: '冲裁', label: '冲裁' },
      { value: '外协', label: '外协' },
      { value: '电脑车', label: '电脑车' },
      { value: '针车', label: '针车' },
      { value: '成型', label: '成型' }
    ]
  },
  mtlProperty: {
    selectValue: '',
    options: [
      { value: '冲裁主料', label: '冲裁主料' },
      { value: '冲裁辅料', label: '冲裁辅料' },
      { value: '针车辅料', label: '针车辅料' }
    ]
  },
  noLotUsage: '',
  remark: '',
  disableConfirm: true,
  change: 'Y'
})

// 选择当前行到正式材料用量表
const currentRow7 = (row: any) => {
  const { mtlColor, mtlName, mtlNo, mtlSpec, mtlUnit, vendor } = row
  SelectTheCurrentRowToInsertMethodsParms.value.mtlColor = mtlColor
  SelectTheCurrentRowToInsertMethodsParms.value.mtlName = mtlName
  SelectTheCurrentRowToInsertMethodsParms.value.mtlNo = mtlNo
  SelectTheCurrentRowToInsertMethodsParms.value.mtlSpec = mtlSpec
  SelectTheCurrentRowToInsertMethodsParms.value.mtlUnit = mtlUnit
  SelectTheCurrentRowToInsertMethodsParms.value.vendor = vendor
}

const SelectTheCurrentRowToInsertMethodsParms = ref<SelectTheCurrentRowToInsertParms>({
  colorCode: '',
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlUnit: '',
  partName: '',
  segment: null,
  seqNo: '',
  styleColor: '',
  styleNo: '',
  vendor: ''
})

const SelectTheCurrentRowToInsertMethods = async () => {
  if (SelectTheCurrentRowToInsertMethodsParms.value.mtlNo === '') {
    JMessage.warning('请选择需要插入的数据')
  } else {
    const result = await SelectTheCurrentRowToInsert(SelectTheCurrentRowToInsertMethodsParms.value)
    if (result.code === 200) {
      TypeColorDataQueryMethods()
      dialogVisible1.value = false
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

// 选择材料
const dialogVisible1 = ref(false)

const findData1 = (count: string) => {
  if (count === '置空') {
    clearFormList1()
  }
  if (count === '选择当前行正式材料到用量表') {
    if (SelectTheCurrentRowToInsertMethodsParms.value.mtlNo === '') {
      JMessage.warning('请选择需要插入的数据')
    } else {
      SelectTheCurrentRowToInsertMethods()
    }
  }
}

const Launchform1 = (row: any) => {
  isLoading1.value = true
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.mtlNo = row[0].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.mtlName = row[1].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.mtlName2 = row[2].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.mtlName3 = row[3].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.mtlColor = row[4].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.vendor = row[5].inputValue
  FormalMaterialsAndMethodsForDevelopingMaterials()
}

const clearFormList1 = () => {
  formList1.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    // Add other conditions for different form item types
  })
}

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入物料编码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入名称1',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入名称2',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入名称3',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入颜色',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入供应商',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '选择当前行正式材料到用量表'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

// 正式材料和开发材料
const FormalMaterialsAndMethodsForDevelopingTableDateList = ref<FormalMaterialsAndDevelopmentMaterialsParms>({
  officialMaterials: {
    adhesiveMaterialList: []
  },
  developmentMaterials: {
    developmentMaterialsList: []
  }
})

const FormalMaterialsAndMethodsForDevelopingMaterialsParms = ref<any>({
  mtlColor: '',
  mtlName: '',
  mtlName2: '',
  mtlName3: '',
  mtlNo: '',
  pageNum: 1,
  pageNum2: 1,
  pageSize: 40,
  pageSize2: 40,
  vendor: ''
})

const totalT = ref(0)

const isLoading1 = ref(true)

const FormalMaterialsAndMethodsForDevelopingMaterials = async () => {
  const result = await FormalMaterialsAndDevelopmentMaterials(FormalMaterialsAndMethodsForDevelopingMaterialsParms.value)
  totalT.value = result.officialMaterials.total!
  FormalMaterialsAndMethodsForDevelopingTableDateList.value.developmentMaterials.developmentMaterialsList =
    result.developmentMaterials.developmentMaterialsList
  FormalMaterialsAndMethodsForDevelopingTableDateList.value.officialMaterials.adhesiveMaterialList = result.officialMaterials.adhesiveMaterialList
  isLoading1.value = false
}

const handleSizeChange1 = (val: number) => {
  isLoading1.value = true
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.pageNum = val
  FormalMaterialsAndMethodsForDevelopingMaterials()
}
const handleCurrentChange1 = (val: number) => {
  isLoading1.value = true
  FormalMaterialsAndMethodsForDevelopingMaterialsParms.value.pageSize = val
  FormalMaterialsAndMethodsForDevelopingMaterials()
}

// 取研发BOM
const colorCodeList = ref('')

const overRide = ref(0)

const FetchResearchBomMethods = async () => {
  if (TypeQueryMethodsParms.value.styleNo === '' || ColorCodeQueryMethodsParms.value.styleColor === '') {
    JMessage.warning('型体颜色名称不可为空')
  } else {
    const result = await FetchResearchBom({
      styleColor: ColorCodeQueryMethodsParms.value.styleColor,
      styleNo: TypeQueryMethodsParms.value.styleNo,
      colorCode: ColorCodeQueryMethodsParms.value.colorCode,
      overRide: overRide.value
    })
    if (result.code === 200) {
      ColorCodeQueryMethodsParms.value.colorCode = ''
      ColorCodeQueryMethodsParms.value.styleColor = ''
      overRide.value = 0
      TypeColorDataQueryMethods()
      ColorCodeQueryMethods()
      TypeQueryMethods()
      JMessage.success(result.message)
    } else if (result.code === 301) {
      handleCloseMateriaLKlc20(result.message)
    } else {
      overRide.value = 0
      TypeColorDataQueryMethods()
      JMessage.error(result.message)
    }
  }
}

const handleCloseMateriaLKlc20 = (cod: string) => {
  overRide.value = 1
  ElMessageBox.confirm(cod)
    .then(async () => {
      FetchResearchBomMethods()
    })
    .catch(() => {})
}

// 重排序号
const ReorderList = async () => {
  const result = await LoginWork.post<any, any>('/Cargo/getReordering', {
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    cfmFlag: cfmFlagList.value
  })
  if (result.code === 200) {
    TypeColorDataQueryMethods()
    JMessage.success(result.message)
  } else {
    TypeColorDataQueryMethods()
    JMessage.error(result.message)
  }
}

// 注意事项建档
const dialogVisible = ref(false)

// 注意事项建档
const PrecautionsFileMethodsParmsDateList = ref([])

// 注意事项建档保存
const inputChange = async (row: any) => {
  delete row.change
  const result = await LoginWork.post<any, any>('/Cargo/getNotesForFilingSave', {
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    list: PrecautionsFileMethodsParmsDateList.value
  })
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const PrecautionsFileMethods = async () => {
  const reuslt = await PrecautionsFileList({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo
  })
  setTimeout(async () => {
    const result = await PrecautionsFile({
      styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
      styleNo: FullCodeUsageCheckMethodsParms.value.styleNo
    })
    PrecautionsFileMethodsParmsDateList.value = result.map((item: any) => {
      item.change = 'Y'
      item.disableConfirm = true
      return item
    })
  }, 1)
}

// 删除全码用量
const listValueDate = ref<any>([])

const currentRow10 = (row: any) => {
  listValueDate.value.push(row)
  listValueDate.value = listValueDate.value.map((item: any) => {
    item.segment = UpdateFullCodeUsageMethodsParmsList.value[0].segment
    item.partName = UpdateFullCodeUsageMethodsParmsList.value[0].partNmae.selectValue
    return item
  })
}

const DeleteTheFullCodeUsage = async () => {
  const result = await LoginWork.post<any, any>('/Cargo/getFullCodeSizeDel', {
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    list: [listValueDate.value.pop()]
  })
  if (result.code === 200) {
    TypeQueryMethods()
    listValueDate.value = []
    JMessage.success(result.message)
  } else {
    TypeQueryMethods()
    listValueDate.value = []
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlcLtvCodeUsage = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      DeleteTheFullCodeUsage()
    })
    .catch(() => {})
}

// 更新全码用量
const UpdateFullCodeUsageMethodsParms = ref<UpdateFullCodeUsagePamrs>({
  list: [],
  styleColor: '',
  styleNo: ''
})

const UpdateFullCodeUsageMethodsParmsList = ref<any>([])

const segMethodsParms = ref<any>({
  segment: '',
  partNmae: ''
})

const UpdateFullCodeUsageMethods = async () => {
  console.log(AllListMethodsTableDateList.value)
  UpdateFullCodeUsageMethodsParms.value.list = AllListMethodsTableDateList.value.map((item: any) => {
    item.segment = segMethodsParms.value.segment
    item.partName = segMethodsParms.value.partNmae.selectValue
    item.mtlUsage = Number(item.mtlUsage)
    item.sizeFrom = Number(item.sizeFrom)
    delete item.change
    delete item.disableConfirm

    return item
  })

  const result = await UpdateFullCodeUsage({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    list: AllListMethodsTableDateList.value
  })
  if (result.code === 200) {
    FullCodeUsageDataMethods()
    UpdateFullCodeUsageMethodsParms.value.list = []
    JMessage.success(result.message)
  } else {
    UpdateFullCodeUsageMethodsParms.value.list = []
    JMessage.error(result.message)
  }
}

// 材料明细删除和新增
const materialsDeletdAndAdd = ref('')

// 删除打勾记录
const selectedList = (row: any) => {
  DeleteTheCurrentRowDataPmars.value.seqNoList = row.map((item: any) => {
    return item.seqNo
  })
}

const DeleteTheCurrentRowDataPmars = ref<DeleteTheCheckmarkedRecordTypePamrs>({
  seqNoList: [],
  styleColor: '',
  styleNo: ''
})

const DeleteTheCurrentRowData = async (row?: any) => {
  const reuslt = await DeleteTheCheckmarkedRecordType({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo,
    seqNoList: DeleteTheCurrentRowDataPmars.value.seqNoList
  })
  if (reuslt.code === 200) {
    AddDetailData.value.mtlNo = ''
    TypeColorDataQueryMethods()
    QueryTheDetailsOfTheFitSubMaterialMethods()
    DeleteTheCurrentRowDataPmars.value.seqNoList = []
    JMessage.success(reuslt.message)
  } else {
    AddDetailData.value.mtlNo = ''
    QueryTheDetailsOfTheFitSubMaterialMethods()
    DeleteTheCurrentRowDataPmars.value.seqNoList = []
    JMessage.error(reuslt.message)
  }
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      DeleteTheCurrentRowData()
    })
    .catch(() => {})
}

// 全码用量查询
const TypeColorDataQueryMethodsPamrs = ref<TypeColorDataQueryParms>({
  styleNo: '',
  styleColor: ''
})

const AllListMethodsParms = ref<FullCodeUsageDataParms>({
  styleNo: '',
  styleColor: '',
  partName: '',
  segment: -1
})

const AllListMethodsTableDateList = ref<any>([])

const FullCodeUsageDataMethods = async () => {
  isLoading5.value = true
  const result = await FullCodeUsageData(AllListMethodsParms.value)
  AllListMethodsTableDateList.value = result
  isLoading5.value = false
}

// 贴合子材料明细查询
const currentRow3 = (row: any) => {
  tableDateList.value = row
  const { mtlNo, partName, segment, seqNo, usage, usageType } = row
  segMethodsParms.value.segment = segment
  segMethodsParms.value.partNmae = partName

  QueryTheDetailsOfTheFitSubMaterialMethodsParms.value.mtlNo = mtlNo
  QueryTheDetailsOfTheFitSubMaterialMethods()

  AllListMethodsParms.value.partName = partName.selectValue
  AllListMethodsParms.value.segment = segment
  FullCodeUsageDataMethods()

  UpdateFullCodeUsageMethodsParmsList.value.push({ partNmae: partName, segment: segment })

  SelectTheCurrentRowToInsertMethodsParms.value.seqNo = seqNo
  SelectTheCurrentRowToInsertMethodsParms.value.segment = segment
  SelectTheCurrentRowToInsertMethodsParms.value.partName = partName.selectValue

  LevelBasicNumberMethodsParms.value.partName = partName.selectValue
  LevelBasicNumberMethodsParms.value.segment = Number(segment)
  LevelBasicNumberMethodsParms.value.usage = Number(usage)
  LevelBasicNumberMethodsParms.value.usageType = usageType.selectValue

  mtlNoYes.value = mtlNo
}

const QueryTheDetailsOfTheFitSubMaterialMethodsParms = ref<any>({
  mtlNo: ''
})

const QueryTheDetailsOfTheFitSubMaterialMethodsParmsTableDateList = ref<LaminatedSubmaterialInterface[]>()

const QueryTheDetailsOfTheFitSubMaterialMethods = async () => {
  isLoading4.value = true
  if (QueryTheDetailsOfTheFitSubMaterialMethodsParms.value.mtlNo === null || QueryTheDetailsOfTheFitSubMaterialMethodsParms.value.mtlNo === '') {
    QueryTheDetailsOfTheFitSubMaterialMethodsParmsTableDateList.value = []
  } else {
    const result = await QueryTheDetailsOfTheFitSubMaterial(QueryTheDetailsOfTheFitSubMaterialMethodsParms.value)
    if (result.length > 0) {
      QueryTheDetailsOfTheFitSubMaterialMethodsParmsTableDateList.value = result.map((item: any) => {
        if (item.purFlag === 'Y') {
          item.purFlag = true
        }
        item.change = 'Y'
        item.disableConfirm = true
        return item
      })
    }
  }

  isLoading4.value = false
}

// 用量明细资料
const TypeColorDataQueryMethodsInterfaceTableDateList = ref<TypeColorDataQueryInterface[]>([])

const TypeColorDataQueryMethods = async () => {
  isLoading3.value = true
  const reuslt = await TypeColorDataQuery({
    styleColor: FullCodeUsageCheckMethodsParms.value.styleColor,
    styleNo: FullCodeUsageCheckMethodsParms.value.styleNo
  })
  if (reuslt.code === 200) {
    TypeColorDataQueryMethodsInterfaceTableDateList.value = reuslt.data.map((item: any) => {
      if (ActiveLis.value) {
        item.change = 'Y'
        item.disableConfirm = true
      } else {
        item.disable = true
      }

      if (item.noLotUsage === 'Y') {
        item.noLotUsage = true
      }
      item.partName = {
        selectValue: item.partName || '',
        options: []
      }
      item.vendor = {
        selectValue: item.vendor || '',
        options: []
      }
      item.usageType = {
        selectValue: item.usageType || '',
        options: []
      }
      item.drawDept = {
        selectValue: item.drawDept || '',
        options: []
      }
      item.mtlProperty = {
        selectValue: item.mtlProperty || '',
        options: []
      }

      return item
    })

    // 检查数据长度并填充空元素
    // 检查数据长度并填充空元素
    if (ActiveLis.value) {
      const requiredLength = 40
      const currentLength = TypeColorDataQueryMethodsInterfaceTableDateList.value.length

      // 计算当前最大 seqNo
      const maxSeqNo = TypeColorDataQueryMethodsInterfaceTableDateList.value.reduce((max: any, item: any) => {
        const seqNo = parseInt(item.seqNo, 10) // 将 seqNo 转换为整数
        return seqNo > max ? seqNo : max // 返回最大值
      }, 0) // 初始值为 0

      if (currentLength < requiredLength) {
        const addItem = requiredLength - currentLength

        for (let index = 0; index < addItem; index++) {
          // 复制 AddDetailData 的值并设置 seqNo
          let newDetailData = { ...AddDetailData.value }
          newDetailData.seqNo = (maxSeqNo + index + 1).toString() // 设置 seqNo 为最大值 + index + 1

          // 将新数据添加到列表中
          TypeColorDataQueryMethodsInterfaceTableDateList.value.push(newDetailData as any)
        }
      }
    }

    if (reuslt.data.length > 0) {
      QueryTheDetailsOfTheFitSubMaterialMethodsParms.value.mtlNo = reuslt.data[0].mtlNo
      AllListMethodsParms.value.partName = reuslt.data[0].partName.selectValue
      AllListMethodsParms.value.segment = reuslt.data[0].segment
      UpdateFullCodeUsageMethodsParmsList.value.push({ partNmae: reuslt.data[0].partName, segment: reuslt.data[0].segment })
      mtlNoYes.value = reuslt.data[0].mtlNo
    }
  }
  QueryTheDetailsOfTheFitSubMaterialMethods()
  FullCodeUsageDataMethods()

  isLoading3.value = false
}

const currentRow2 = (row: any) => {
  const { styleColor, cfmFlag, colorCode, remark } = row
  TypeColorDataQueryMethodsPamrs.value.styleColor = styleColor
  AllListMethodsParms.value.styleColor = styleColor
  FullCodeUsageCheckMethodsParms.value.styleColor = styleColor
  SelectTheCurrentRowToInsertMethodsParms.value.styleColor = styleColor
  LevelBasicNumberMethodsParms.value.styleColor = styleColor
  ubdateSizeNoMethodsParms.value.styleColor = styleColor
  FullCodeUsageCheckMethodsParms.value.cfmFlag = cfmFlag
  formList.value[23].inputValue = row.remark
  bomExportParms.value.styleColor = styleColor
  bomExportParms.value.colorCode = colorCode
  TypeColorDataQueryMethods()
  colorCodeList.value = colorCode
  cfmFlagListDelete.value = cfmFlag
  if (cfmFlag !== 'Y') {
    ActiveLis.value = true
  } else {
    ActiveLis.value = false
  }
}

// 型体
const styleNoList = ref('')

const ColorCodePictureMethodsParmsTableDateList = ref<ColorCodePictureTableDate>({
  list: []
})

const ColorCodeQueryMethodsParms = ref<ColorCodePictureParms>({
  colorCode: '',
  pageNum: 1,
  pageSize: 40,
  styleColor: '',
  styleNo: ''
})

const cfmFlagList = ref()

const ActiveLis = ref(false)

const ColorCodeQueryMethods = async () => {
  isLoading2.value = true
  const result = await ColorCodeQuery(ColorCodeQueryMethodsParms.value)
  ColorCodePictureMethodsParmsTableDateList.value.list = result.list.map((item: any) => {
    if (item.cfmFlag === 'Y') {
      item.backgroundColor = '#80FF00'
      cfmFlagList.value = 'Y'
    } else {
      cfmFlagList.value = null
      ActiveLis.value = true
      item.change = 'Y'
      item.disableConfirm = true
    }
    return item
  })
  imgList.value = result.list.map((item: any) => {
    return item.photoPath
  })
  total.value = result.total!
  if (result.list.length > 0) {
    if (result.list[0].cfmFlag !== 'Y') {
      ActiveLis.value = true
    } else {
      ActiveLis.value = false
    }
    TypeColorDataQueryMethodsPamrs.value.styleColor = result.list[0].styleColor
    AllListMethodsParms.value.styleColor = result.list[0].styleColor
    FullCodeUsageCheckMethodsParms.value.styleColor = result.list[0].styleColor
    SelectTheCurrentRowToInsertMethodsParms.value.styleColor = result.list[0].styleColor
    LevelBasicNumberMethodsParms.value.styleColor = result.list[0].styleColor
    ubdateSizeNoMethodsParms.value.styleColor = result.list[0].styleColor
    FullCodeUsageCheckMethodsParms.value.cfmFlag = result.list[0].cfmFlag
    formList.value[23].inputValue = result.list[0].remark
    colorCodeList.value = result.list[0].colorCode
    bomExportParms.value.styleColor = result.list[0].styleColor
    bomExportParms.value.colorCode = result.list[0].colorCode

    TypeColorDataQueryMethods()
  }
  isLoading2.value = false
}

const total = ref(0)

const handleSizeChange = (val: number) => {
  ColorCodeQueryMethodsParms.value.pageSize = val
  ColorCodeQueryMethods()
}

const handleCurrentChange = (val: number) => {
  ColorCodeQueryMethodsParms.value.pageNum = val
  ColorCodeQueryMethods()
}

const currentRow1 = (row: any) => {
  const { style } = row
  ColorCodeQueryMethodsParms.value.styleNo = style
  TypeColorDataQueryMethodsPamrs.value.styleNo = style
  AllListMethodsParms.value.styleNo = style
  FullCodeUsageCheckMethodsParms.value.styleNo = style
  SelectTheCurrentRowToInsertMethodsParms.value.styleNo = style
  LevelBasicNumberMethodsParms.value.styleNo = style
  GetColorOrColorCodeAndImagParms.value.styleNo = style
  ubdateSizeNoMethodsParms.value.styleNo = style
  bomExportParms.value.styleColor = style

  ColorCodeQueryMethods()
  GetColorOrColorCodeAndImagMethods()
}

// 审核取消检核
const FullNnumer = ref()

const FullCodeUsageCheckMethodsParms = ref<any>({
  styleColor: '',
  styleNo: '',
  cfmFlag: ''
})

const FullCodeUsageCheckMethods = async () => {
  FullCodeUsageCheckMethodsParms.value.cfmFlag = FullCodeUsageCheckMethodsParms.value.cfmFlag === 'Y' ? 'Y' : ''
  const result = await FullCodeUsageCheck(FullNnumer.value, FullCodeUsageCheckMethodsParms.value)
  if (result.code === 200) {
    ColorCodeQueryMethods()
    JMessage.success(result.message)
  } else {
    ColorCodeQueryMethods()
    JMessage.error(result.message)
  }
}

const bomExportParms = ref({
  colorCode: '',
  companyId: '',
  pageNum: '',
  pageSize: '',
  styleColor: '',
  styleNo: ''
})

const findData = async (count: any) => {
  if (count === 'BOM表导出') {
    const res = (await fileRequest.post(`/Cargo/bomExport`, bomExportParms.value)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (count === '保存型体基本号码') {
    if (ubdateSizeNoMethodsParms.value.baseSize === '') {
      JMessage.warning('型体基本号码不可为空')
    } else {
      ubdateSizeNoMethods()
    }
  }
  if (count === '查询') {
    TypeQueryMethods()
  }
  if (count === '取消检核') {
    FullNnumer.value = 1
    FullCodeUsageCheckMethods()
  }
  if (count === '检核') {
    FullNnumer.value = 0
    FullCodeUsageCheckMethods()
  }
  if (count === '删除勾选部位资料') {
    materialsDeletdAndAdd.value = '删除勾选部位资料'
    if (DeleteTheCurrentRowDataPmars.value.seqNoList.length === 0) {
      JMessage.warning('请选择需要删除的记录')
    } else {
      handleCloseMateriaLKlc()
    }
  }
  if (count === '更新全码用量') {
    UpdateFullCodeUsageMethods()
  }
  if (count === '新增全码用量') {
    AllListMethodsTableDateList.value.push({ sizeFrom: '', mtlUsage: '', change: 'Y', disableConfirm: 'true' })
  }
  if (count === '删除全码用量') {
    if (listValueDate.value.length === 0) {
      JMessage.warning('请选择需要删除全码用量数据')
    } else {
      handleCloseMateriaLKlcLtvCodeUsage()
    }
  }
  if (count === '注意事项建档') {
    PrecautionsFileMethods()
    dialogVisible.value = true
  }
  if (count === '重排序号') {
    ReorderList()
  }
  if (count === '取研发BOM') {
    FetchResearchBomMethods()
  }
  if (count === '选择材料') {
    if (SelectTheCurrentRowToInsertMethodsParms.value.seqNo === '') {
      JMessage.warning('请选择需要修改的数据')
    } else {
      dialogVisible1.value = true
      FormalMaterialsAndMethodsForDevelopingMaterials()
    }
  }
  if (count === '复制用量') {
    ReplicationVolumeQuery()
    dialogVisible5.value = true
  }
  if (count === 'BOM资料删除') {
    console.log(cfmFlagListDelete.value)
    if (cfmFlagListDelete.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseMateriaLKlcLtvCodeUsage1()
    }
  }
  if (count === '转型体资料建档') {
    router.push({
      path: '/ControlDepartment/MaterialManagement/BulkBodyFile'
    })
  }
  if (count === '转指令资料输入') {
    router.push({
      path: '/ControlDepartment/FactoryProduction/InstructionDataEntry'
    })
  }
  if (count === '级放部位全码用量') {
    if (LevelBasicNumberMethodsParms.value.baseSize === '') {
      JMessage.warning('基本号码不可为空')
    } else if (LevelBasicNumberMethodsParms.value.segment === -1) {
      JMessage.warning('请选择需要级放号码的数据')
    } else {
      handleCloseMateriaLKlcLtvCodeUsage2()
    }
  }
  if (count === '增加明细资料') {
    if (ActiveLis.value) {
      if (tableDateList.value) {
        let newDetailData = { ...AddDetailData.value }
        let index = TypeColorDataQueryMethodsInterfaceTableDateList.value.findIndex((item) => item.seqNo === tableDateList.value.seqNo)

        if (index !== -1) {
          // 设置新元素的 seqNo
          newDetailData.seqNo = (Number(tableDateList.value.seqNo) + 1) as any

          // 在找到的索引后插入新数据
          TypeColorDataQueryMethodsInterfaceTableDateList.value.splice(index + 1, 0, newDetailData as any)

          // 更新插入位置之后的所有元素的 seqNo
          for (let i = index + 2; i < TypeColorDataQueryMethodsInterfaceTableDateList.value.length; i++) {
            TypeColorDataQueryMethodsInterfaceTableDateList.value[i].seqNo =
              Number(TypeColorDataQueryMethodsInterfaceTableDateList.value[i].seqNo) + 1
          }

          // 调用获取供应商列表方法
          GetVendorListMethods()

          // 调用查询明细资料方法
          PositionDetailQueryMehtods()
        }
      } else {
        // 复制新数据对象
        let newDetailData = { ...AddDetailData.value }

        // 获取列表中已有的最大序号
        let maxSeqNo = Math.max(...TypeColorDataQueryMethodsInterfaceTableDateList.value.map((item: any) => item.seqNo), 0)

        // 设置新数据对象的序号为最大序号加1
        newDetailData.seqNo = (maxSeqNo + 1) as any

        // 将新条目添加到列表中
        TypeColorDataQueryMethodsInterfaceTableDateList.value.push(newDetailData as any)

        // 更新列表数据
        TypeColorDataQueryMethodsInterfaceTableDateList.value = [...TypeColorDataQueryMethodsInterfaceTableDateList.value]

        // 重置查询参数
        PositionDetailQueryMethodsParms.value.engPartName = ''
        // PositionDetailQueryMethodsParms.value.partName = '';
        PositionDetailQueryMethodsParms.value.seqNo = ''

        // 调用获取供应商列表方法
        GetVendorListMethods()

        // 调用查询明细资料方法
        PositionDetailQueryMehtods()
      }
    } else {
      JMessage.warning('已检核的数据不可修改')
    }
  }
  if (count === '保存') {
    saveDateListMethods()
  }
}

const router = useRouter()

// 型体查询
const imgList = ref<any>([])

const Launchform = (row: any) => {
  TypeQueryMethodsParms.value.styleNo = row[0].inputValue
  ColorCodeQueryMethodsParms.value.colorCode = row[1].inputValue
  ColorCodeQueryMethodsParms.value.styleColor = row[2].inputValue
  GetColorOrColorCodeAndImagParms.value.styleNo = row[0].inputValue
  ubdateSizeNoMethodsParms.value.baseSize = row[23].inputValue

  LevelBasicNumberMethodsParms.value.baseSize = Number(row[8].inputValue)
}

const TypeQueryMethodsParms = ref<TypeQueryParms>({
  colorCode: '',
  pageNum: 1,
  pageSize: 80,
  styleColor: '',
  styleNo: ''
})

const TypeQueryMethodsTableDateList = ref<any>([])

const TypeQueryMethods = async () => {
  isLoading.value = true
  const result = await TypeQuery(TypeQueryMethodsParms.value)
  totalTk.value = result.total
  // if(result.code === 504){

  //   return
  // }

  if (result.data !== null && result.list.length > 0) {
    TypeQueryMethodsTableDateList.value = result.list.map((item: any) => {
      return { style: item }
    })
    ColorCodeQueryMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    styleNoList.value = TypeQueryMethodsTableDateList.value[0].style
    TypeColorDataQueryMethodsPamrs.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    AllListMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    FullCodeUsageCheckMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    SelectTheCurrentRowToInsertMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    LevelBasicNumberMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    GetColorOrColorCodeAndImagParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    ubdateSizeNoMethodsParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    bomExportParms.value.styleNo = TypeQueryMethodsTableDateList.value[0].style
    GetColorOrColorCodeAndImagMethods()
    ColorCodeQueryMethods()
  } else {
    JMessage.error(result.message)
  }
  isLoading.value = false
}

const totalTk = ref(0)

const handleSizeChangeTk = (val: number) => {
  TypeQueryMethodsParms.value.pageSize = val
  TypeQueryMethods()
}

const handleCurrentChangeTk = (val: number) => {
  TypeQueryMethodsParms.value.pageNum = val
  TypeQueryMethods()
}

onMounted(() => {
  TypeQueryMethods()

  GetVendorListMethods()
})

// 表格数据
const tableHeader = ref([
  {
    label: '型体',
    prop: 'style'
  }
])

// 表格数据

const tableHeader1 = ref([
  {
    label: '代码',
    prop: 'colorCode',
    width: '80',
    change: true
  },
  {
    label: '颜色',
    width: '80',
    prop: 'styleColor',
    change: true,
    backgroundColor: true
  },
  {
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    type: 'img',
    srcList: imgList
  }
])

// 表格数据
const tableHeader3 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    change: true
  },
  {
    label: '部位',
    prop: 'partName',
    type: 'select',
    disable: true
  },
  {
    label: '分段',
    prop: 'segment',
    change: true
  },
  {
    label: '分类名称',
    prop: 'typeName',
    change: true,
    start: true
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    width: '150'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true,
    width: '200',
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '尺码起',
    prop: 'size1'
  },
  {
    label: '尺码止',
    prop: 'size2'
  },
  {
    label: '供应商',
    prop: 'vendor',
    type: 'select',
    disable: true
  },
  {
    label: '用量',
    prop: 'usage'
  },
  {
    label: '计算方式',
    prop: 'usageType',
    type: 'select',
    disable: true
  },
  // {
  //   label: '领用部门',
  //   prop: 'drawDept',
  //   type: 'select',
  //   disable: true,
  // },
  // {
  //   label: '捡料',
  //   prop: 'noLotUsage',
  //   type: 'checkbox',
  // },
  // {
  //   label: '主辅料',
  //   prop: 'mtlProperty',
  //   type: 'select',
  //   disable: true,
  // },
  {
    label: '备注',
    prop: 'remark'
  }
])

// 表格数据
const tableHeader5 = ref([
  {
    label: '分类名称',
    prop: 'colorCode',
    width: '80',
    change: true
  },
  {
    label: '物料编码',
    width: '80',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '加工方式',
    prop: 'prdType'
  },
  {
    label: '比例',
    prop: 'percent'
  },
  {
    label: '是否请购',
    prop: 'purFlag',
    type: 'checkbox'
  }
])

// 表格数据
const tableHeader6 = ref([
  {
    label: '号码',
    prop: 'sizeFrom'
  },
  {
    label: '用量',
    prop: 'mtlUsage'
  }
])

const tableHeader19 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    change: true,
    width: '90'
  },
  {
    label: '事项名称',
    prop: 'seqName',
    change: true,
    width: '90'
  },
  {
    label: '注意事项',
    prop: 'prodMemo',
    textarea: true
  }
])

const tableHeader2 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: '300'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '供应商',
    prop: 'vendor'
  }
])

const tableHeader16 = ref([
  {
    label: '型体',
    prop: 'styleNo'
  },
  {
    label: '颜色',
    prop: 'styleColor'
  },
  {
    label: '冲刀号(起)',
    prop: 'minCutSize'
  },
  {
    label: '冲刀号(止)',
    prop: 'maxCutSize'
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}

.MainRightLeft {
  display: flex;
}

.MainLeft {
  width: 15%;
}

.MainRight {
  margin-left: 20px;
}
</style>
